# 最低版本要求
cmake_minimum_required(VERSION 3.10)

# 项目信息
project(demo)

# 添加公共头文件目录
include_directories(include)

# 添加静态库
add_library(slib STATIC src/slib.cpp include/slib.cpp)

# 添加动态库
add_library(dlib SHARED src/dlib.cpp include/dlib.cpp)

# 寻找Opencv库, REQUIRED表示必须存在，否则报错
set(OpenCV_DIR /home/luoluoluo/yoloCpp/3rdparty/opencv4.8_release/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)
# 配置项目包含头文件和库文件
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARIES_DIRS})

# 添加可执行文件
add_executable(${PROJECT_NAME} src/main.cpp)

# 为可执行文件添加头文件目录和库文件目录
target_link_libraries(${PROJECT_NAME} ${Opencv_LIBS})

# 设置RPATH解决install后无法找到动态库的问题
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}.lib")

# 链接库
target_link_libraries(${PROJECT_NAME} slib dlib)

# 安装头文件目录
# 方法一: 使用install将文件夹安装到指定目录
# install(DIRECTORY include/ DESTINATION include)
# 方法二: 设置公共头文件:PUBLIC_HEADER
set_target_properties(slib PROPERTIES PUBLIC_HEADER include/slib.h)
set_target_properties(dlib PROPERTIES PUBLIC_HEADER include/dlib.h)

# 安装
# bin、lib有前缀: CMAKE_INSTALL_PREFIX, 默认值为usr/local
install(
    TARGETS ${PROJECT_NAME} slib dlib
    RUNTIME DESTINATION bin # 可执行文件
    LIBRARY DESTINATION lib # 动态库
    ARCHIVE DESTINATION lib # 静态库
    PUBLIC_HEADER DESTINATION include # 头文件 对应头文件安装目录方法二
)

#[[
运行方式
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=./installed
cmake --build build
cmake --install build
]]
